<?php

function get_video_list($con, $paradise_id = NULL, $limit = NULL, $keyword = NULL)
{
	$query = "SELECT * FROM video";
	
	if ($paradise_id != NULL || $keyword != NULL) {
		$query .= " WHERE";
		
		if ($paradise_id != NULL && $keyword != NULL) {
			$query .= " paradise_id = '$paradise_id' AND title LIKE '%$keyword%'";
		}
		else if ($paradise_id != NULL) {
			$query .= " paradise_id = '$paradise_id'";
		}
		else if ($keyword != NULL) {
			$query .= " title LIKE '%$keyword%'";
		}
	}
	
	$query .= " ORDER BY id DESC";
	
	if ($limit !== FALSE) {
		$query .= " LIMIT $limit";
	}
	
	return select($con, $query);
}

function get_video($con, $id)
{
	$query = "SELECT * FROM video WHERE id = '$id'";
	$records = select($con, $query);
	if(count($records) == 1) {
		return $records[0];
	}
	else {
		return NULL;
	}
}

function insert_video($con, $video)
{
	$title			= $video["title"];
	$description	= $video["description"];
	$paradise_id	= $video["paradise_id"];
	$user_id		= $video["user_id"];
	$youtube_id		= $video["youtube_id"];
	
	$query = "INSERT INTO video(title, description, paradise_id, user_id, youtube_id) " .
		"VALUES ('$title', '$description', '$paradise_id', '$user_id', '$youtube_id')";
	
	return insert($con, $query);
}

function update_video($con, $video)
{
	$id				= $video["id"];
	$title			= $video["title"];
	$description	= $video["description"];
	$paradise_id	= $video["paradise_id"];
	$user_id		= $video["usre_id"];
	$youtube_id		= $video["youtube_id"];
		
	$query = "UPDATE video SET title = '$title', description = '$description', " .
		"paradise_id = '$paradise_id', user_id = '$user_id', youtube_id = '$youtube_id' " .
		"WHERE id = '$id'";
	
	return update($con, $query);
}
